<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>淘宝系统 - 登录注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            width: 400px;
            max-width: 90vw;
        }
        
        .header {
            background: linear-gradient(135deg, #ff6b6b, #ee5a24);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 28px;
            margin-bottom: 10px;
        }
        
        .header p {
            opacity: 0.9;
            font-size: 14px;
        }
        
        .form-container {
            padding: 30px;
        }
        
        .tab-container {
            display: flex;
            margin-bottom: 30px;
            border-bottom: 2px solid #f0f0f0;
        }
        
        .tab {
            flex: 1;
            padding: 15px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            border-bottom: 3px solid transparent;
        }
        
        .tab.active {
            color: #ff6b6b;
            border-bottom-color: #ff6b6b;
        }
        
        .form {
            display: none;
        }
        
        .form.active {
            display: block;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #ff6b6b;
        }
        
        .form-row {
            display: flex;
            gap: 10px;
        }
        
        .form-row .form-group {
            flex: 1;
        }
        
        .verification-code-group {
            display: flex;
            gap: 10px;
        }
        
        .verification-code-group input {
            flex: 1;
        }
        
        .verification-code-group button {
            padding: 12px 15px;
            background: #ff6b6b;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            transition: background 0.3s ease;
        }
        
        .verification-code-group button:hover {
            background: #ee5a24;
        }
        
        .verification-code-group button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        
        .checkbox-group {
            display: flex;
            align-items: center;
            gap: 8px;
            margin-bottom: 20px;
        }
        
        .checkbox-group input[type="checkbox"] {
            width: auto;
        }
        
        .submit-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #ff6b6b, #ee5a24);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.3s ease;
        }
        
        .submit-btn:hover {
            transform: translateY(-2px);
        }
        
        .submit-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
        }
        
        .message {
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        
        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #ff6b6b;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>淘宝系统</h1>
            <p>欢迎来到我们的电商平台</p>
        </div>
        
        <div class="form-container">
            <div class="tab-container">
                <div class="tab active" onclick="switchTab('login')">登录</div>
                <div class="tab" onclick="switchTab('register')">注册</div>
            </div>
            
            <!-- 登录表单 -->
            <form class="form active" id="loginForm">
                <div class="message" id="loginMessage" style="display: none;"></div>
                
                <div class="form-group">
                    <label for="loginUsername">用户名</label>
                    <input type="text" id="loginUsername" name="username" required>
                </div>
                
                <div class="form-group">
                    <label for="loginPassword">密码</label>
                    <input type="password" id="loginPassword" name="password" required>
                </div>
                
                <div class="checkbox-group">
                    <input type="checkbox" id="rememberMe" name="rememberMe">
                    <label for="rememberMe">记住我</label>
                </div>
                
                <button type="submit" class="submit-btn" id="loginBtn">登录</button>
                
                <div class="loading" id="loginLoading">
                    <div class="spinner"></div>
                    <p>登录中...</p>
                </div>
            </form>
            
            <!-- 注册表单 -->
            <form class="form" id="registerForm">
                <div class="message" id="registerMessage" style="display: none;"></div>
                
                <div class="form-group">
                    <label for="registerUsername">用户名</label>
                    <input type="text" id="registerUsername" name="username" required>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label for="registerEmail">邮箱</label>
                        <input type="email" id="registerEmail" name="email" required>
                    </div>
                    <div class="form-group">
                        <label for="registerPhone">手机号</label>
                        <input type="tel" id="registerPhone" name="phone">
                    </div>
                </div>
                
                <div class="form-group">
                    <label for="registerPassword">密码</label>
                    <input type="password" id="registerPassword" name="password" required>
                </div>
                
                <div class="form-group">
                    <label for="confirmPassword">确认密码</label>
                    <input type="password" id="confirmPassword" name="confirmPassword" required>
                </div>
                
                <div class="form-group">
                    <label for="verificationCode">验证码</label>
                    <div class="verification-code-group">
                        <input type="text" id="verificationCode" name="verificationCode" placeholder="请输入验证码" required>
                        <button type="button" id="sendCodeBtn" onclick="sendVerificationCode()">发送验证码</button>
                    </div>
                </div>
                
                <button type="submit" class="submit-btn" id="registerBtn">注册</button>
                
                <div class="loading" id="registerLoading">
                    <div class="spinner"></div>
                    <p>注册中...</p>
                </div>
            </form>
        </div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:8888/api';
        
        // 切换表单
        function switchTab(tab) {
            // 更新标签页状态
            document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
            event.target.classList.add('active');
            
            // 更新表单显示
            document.querySelectorAll('.form').forEach(f => f.classList.remove('active'));
            if (tab === 'login') {
                document.getElementById('loginForm').classList.add('active');
            } else {
                document.getElementById('registerForm').classList.add('active');
            }
            
            // 清除消息
            clearMessages();
        }
        
        // 显示消息
        function showMessage(formId, message, type) {
            const messageEl = document.getElementById(formId + 'Message');
            messageEl.textContent = message;
            messageEl.className = `message ${type}`;
            messageEl.style.display = 'block';
        }
        
        // 清除消息
        function clearMessages() {
            document.querySelectorAll('.message').forEach(m => m.style.display = 'none');
        }
        
        // 显示/隐藏加载状态
        function setLoading(formId, loading) {
            const loadingEl = document.getElementById(formId + 'Loading');
            const submitBtn = document.getElementById(formId === 'login' ? 'loginBtn' : 'registerBtn');
            
            if (loading) {
                loadingEl.style.display = 'block';
                submitBtn.disabled = true;
            } else {
                loadingEl.style.display = 'none';
                submitBtn.disabled = false;
            }
        }
        
        // 发送验证码
        async function sendVerificationCode() {
            const email = document.getElementById('registerEmail').value;
            if (!email) {
                showMessage('register', '请先输入邮箱地址', 'error');
                return;
            }
            
            const btn = document.getElementById('sendCodeBtn');
            btn.disabled = true;
            btn.textContent = '发送中...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/auth/send-code`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        email: email,
                        type: 1
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showMessage('register', '验证码已发送到您的邮箱', 'success');
                    // 倒计时
                    let countdown = 60;
                    const timer = setInterval(() => {
                        btn.textContent = `${countdown}秒后重试`;
                        countdown--;
                        if (countdown < 0) {
                            clearInterval(timer);
                            btn.disabled = false;
                            btn.textContent = '发送验证码';
                        }
                    }, 1000);
                } else {
                    showMessage('register', result.message || '发送失败，请稍后重试', 'error');
                    btn.disabled = false;
                    btn.textContent = '发送验证码';
                }
            } catch (error) {
                showMessage('register', '网络错误，请稍后重试', 'error');
                btn.disabled = false;
                btn.textContent = '发送验证码';
            }
        }
        
        // 登录表单提交
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            clearMessages();
            setLoading('login', true);
            
            const formData = new FormData(e.target);
            const data = {
                username: formData.get('username'),
                password: formData.get('password'),
                rememberMe: formData.get('rememberMe') === 'on'
            };
            
            try {
                const response = await fetch(`${API_BASE_URL}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showMessage('login', '登录成功！正在跳转...', 'success');
                    // 保存会话令牌
                    localStorage.setItem('sessionToken', result.sessionToken);
                    localStorage.setItem('userInfo', JSON.stringify({
                        userId: result.userId,
                        username: result.username,
                        email: result.email,
                        avatar: result.avatar
                    }));
                    
                    // 跳转到主页
                    setTimeout(() => {
                        window.location.href = '/dashboard.html';
                    }, 1500);
                } else {
                    showMessage('login', result.message || '登录失败', 'error');
                }
            } catch (error) {
                showMessage('login', '网络错误，请稍后重试', 'error');
            } finally {
                setLoading('login', false);
            }
        });
        
        // 注册表单提交
        document.getElementById('registerForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            clearMessages();
            setLoading('register', true);
            
            const formData = new FormData(e.target);
            const data = {
                username: formData.get('username'),
                password: formData.get('password'),
                confirmPassword: formData.get('confirmPassword'),
                email: formData.get('email'),
                phone: formData.get('phone'),
                verificationCode: formData.get('verificationCode')
            };
            
            try {
                const response = await fetch(`${API_BASE_URL}/auth/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showMessage('register', '注册成功！正在自动登录...', 'success');
                    // 保存会话令牌
                    localStorage.setItem('sessionToken', result.sessionToken);
                    localStorage.setItem('userInfo', JSON.stringify({
                        userId: result.userId,
                        username: result.username,
                        email: result.email,
                        avatar: result.avatar
                    }));
                    
                    // 跳转到主页
                    setTimeout(() => {
                        window.location.href = '/dashboard.html';
                    }, 1500);
                } else {
                    showMessage('register', result.message || '注册失败', 'error');
                }
            } catch (error) {
                showMessage('register', '网络错误，请稍后重试', 'error');
            } finally {
                setLoading('register', false);
            }
        });
        
        // 检查是否已登录
        window.addEventListener('load', () => {
            const sessionToken = localStorage.getItem('sessionToken');
            if (sessionToken) {
                // 验证会话
                fetch(`${API_BASE_URL}/auth/validate-session`, {
                    method: 'POST',
                    headers: {
                        'Authorization': `Bearer ${sessionToken}`
                    }
                }).then(response => response.json())
                .then(result => {
                    if (result.success) {
                        // 已登录，跳转到主页
                        window.location.href = '/dashboard.html';
                    } else {
                        // 会话无效，清除本地存储
                        localStorage.removeItem('sessionToken');
                        localStorage.removeItem('userInfo');
                    }
                }).catch(() => {
                    localStorage.removeItem('sessionToken');
                    localStorage.removeItem('userInfo');
                });
            }
        });
    </script>
</body>
</html> 